#include <iostream>
#include <stdlib.h>
#include "pine.h"
#include "log4z.h"

int main() {
  ILog4zManager::getInstance()->setLoggerLimitsize(LOG4Z_MAIN_LOGGER_ID, 2);
  ILog4zManager::getInstance()->setLoggerLevel(LOG4Z_MAIN_LOGGER_ID, LOG_LEVEL_TRACE);

  TcpServer *server = new TcpServer();

  Signal::signal(SIGINT, [&] {
    delete server;
    std::cout << "\nServer exit!" << std::endl;
    exit(0);
  });

  server->onConnect([](Connection *conn) {
    //  std::cout << "New connection fd: " << conn->socket()->fd() << "[" << conn->socket()->get_addr() << "]" <<  std::endl; 
     LOGFMTA("New connection fd: %d [%s]",conn->socket()->fd(),conn->socket()->get_addr().c_str());
     });

  server->onRecv([](Connection *conn) {
    // std::cout << "Message from client " << conn->read_buf()->c_str() << std::endl;
    LOGFMTA("Message from client :%s",conn->read_buf()->c_str());
    if(conn->read_buf()->Size()){
      conn->Send(conn->read_buf()->c_str());
    }
  });

  server->Start();

  delete server;
  return 0;
}
